package com.oa.android.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.oa.android.R;
import com.oa.android.adapter.TPAdapter;
import com.oa.android.entry.Vote;
import com.oa.android.entry.VoteTp;
import com.oa.android.utils.UConstants;
import com.oa.android.utils.Util;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


/**
 *
 * 投票
 * @author -zouqh
 * 
 */
public class VoteActivity extends Activity implements OnClickListener {

	private ImageView close;
	private TextView title,nat,rigthB;

	private ListView listView;
	private TPAdapter adapter;
	private List<VoteTp> tpList;
	private Vote voteTp;
	private String[] strName;
	private String[] strNum;
	private String[] tpAcount;
	private int temp =-1;
	ProgressDialog dialog;
	private int tpSum;//拿到多选投票的次数
	private int sumAdd;//统计当前是否到达次数
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.vote_activity);
		voteTp= (Vote) getIntent().getSerializableExtra("vote");
		close = (ImageView) findViewById(R.id.close);
		title = (TextView) findViewById(R.id.title_text);
		nat= (TextView) findViewById(R.id.title);
		rigthB= (TextView) findViewById(R.id.ritgh_text);
		listView=(ListView)findViewById(R.id.listView);
		nat.setText(voteTp.getTITLE());
		title.setText("投票");

		rigthB.setVisibility(View.VISIBLE);

		rigthB.setOnClickListener(this);
		close.setOnClickListener(this);
		tpList=new ArrayList<>();
		if (null!=voteTp){

			if (Util.NoNull(voteTp.getCONTENT())&&Util.NoNull(voteTp.getSCORE())){
				if (voteTp.getCONTENT().contains("|")){
					strName=voteTp.getCONTENT().split("\\|");
					strNum=voteTp.getSCORE().split("\\|");
					tpAcount=voteTp.getEMPIDS().split("\\|");
					System.out.println("tpAcount.length===="+tpAcount.length);
				}
			}
		}
		if (null!=strName&&null!=strNum){
			for (int i=0;i<strName.length;i++){
				VoteTp v=new VoteTp();
				v.setId(i);
				v.setName(strName[i]);
				v.setNumber(strNum[i]);
				v.setTpName(tpAcount[i]);
				if (null!=tpAcount[i]&&tpAcount[i].contains(UConstants.EmpId)){
					v.setSelect(true);
					sumAdd++;
				}else{
					v.setSelect(false);
				}

				tpList.add(v);
			}
			if (Util.NoNull(voteTp.getXZ())){
				tpSum=Integer.parseInt(voteTp.getXZ());
				if(tpSum==1){
					rigthB.setText("可投1人");
				}else{
					if (tpSum>strName.length){
						rigthB.setText("可投"+strName.length+"人");
					}else{
						rigthB.setText("可投"+tpSum+"人");
					}
				}
			}else{
				rigthB.setText("可投1人");
			}
//			if (voteTp.getCFBZ().equals("1")){
//				if (voteTp.getZT().equals("0")){
//					rigthB.setVisibility(View.VISIBLE);
//				}
//
//			}else{
//				if (sumAdd<tpSum){
//					rigthB.setVisibility(View.VISIBLE);
//				}
//
//			}
		}
		adapter=new TPAdapter(this,tpList);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

				if (voteTp.getCFBZ().equals("1")){
					if (voteTp.getZT().equals("0")){
						temp=position;
						sendVote(temp);

//						adapter.notifyDataSetChanged();
					}else{
						Toast.makeText(VoteActivity.this, "已投过", Toast.LENGTH_SHORT).show();
					}

				}else{
					if (sumAdd<tpSum){
						if (!tpList.get(position).isSelect()){
							temp=position;
							sendVote(temp);
						}

					}else{
						Toast.makeText(VoteActivity.this, "已投过", Toast.LENGTH_SHORT).show();
					}

				}


			}
		});

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		Intent intent ;
		switch (v.getId()) {
		case R.id.close:
				finish();
				break;
//			case R.id.ritgh_text:
//				if (temp>=0){
//					sendVote(temp);
//				}else{
//					Toast.makeText(getApplicationContext(),"请选择投票人！",Toast.LENGTH_SHORT).show();
//				}
//				break;

		default:
			break;
		}
	}



	//投票
	private void sendVote(final int positon) {
		dialog = ProgressDialog.show(this, null, "投票中...", true, false);
		RequestParams params = new RequestParams(UConstants.SEND_VOTE);
		params.addBodyParameter("LKind", "OA");
		params.addBodyParameter("EmpId", UConstants.EmpId);
		params.addBodyParameter("XH",temp+"");
		params.addBodyParameter("ID", voteTp.getID());
		x.http().get(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				Util.printLog("getMerchants------------onSuccess-----", result);
				if (result.length() > 0) {
					try {
						JSONObject object = new JSONObject(result);

						boolean success = object.getBoolean("success");
						dialog.dismiss();
						if (success) {
							int num=Integer.parseInt(tpList.get(positon).getNumber())+1;
							tpList.get(positon).setNumber(num+"");
							tpList.get(positon).setSelect(true);
							adapter.notifyDataSetChanged();
							Toast.makeText(x.app(), "投票成功！", Toast.LENGTH_LONG).show();
							voteTp.setZT("1");
							if (voteTp.getCFBZ().equals("2")){
								sumAdd++;
							}else{
								rigthB.setVisibility(View.GONE);
							}
							temp=-1;
							VoteListActivity.activity.setResh();
						} else {
							Toast.makeText(x.app(),object.getString("msg")+"", Toast.LENGTH_LONG).show();
						}
//
					} catch (JSONException e) {
						dialog.dismiss();
						e.printStackTrace();
					}
				}

			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
				Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

				Toast.makeText(x.app(), "获取数据失败！", Toast.LENGTH_LONG).show();
				dialog.dismiss();
			}

			@Override
			public void onCancelled(CancelledException cex) {

				Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
				Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
				dialog.dismiss();
			}

			@Override
			public void onFinished() {

			}
		});
	}
}
